package cn.jdy.plugins.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.entity.Product;
import cn.jdy.plugins.service.ProductService;

/**
 * @title 产品控制类
 */
@Controller
@RequestMapping("/product")
public class ProductCtrl extends BaseCtrl {
  @Autowired
  private ProductService productService;
  @Autowired
  private ConfService confService;

  /**
   * 查询产品列表
   * @param product
   * @return
   */
  @RequestMapping("/{typeid}.html")
  public ModelAndView query(Product product,@PathVariable Long typeid) {
    ModelAndView mv = new ModelAndView("/product/productList.jsp");
    try {
      //设置分页信息
      if(product.getiDisplayLength() == 0) product.setiDisplayLength(9);                //默认长度5
      if(product.getPageNo()<=0) product.setPageNo(1);                                  //默认页数1
      product.setiDisplayStart((product.getPageNo() - 1) * product.getiDisplayLength());  //显示起始索引

      product.setStatus("1");//设置状态 
      product.setColumnId(typeid);// 产品分类
	  
      mv.addObject("dataList", productService.query(product));
      mv.addObject("pageNo", product.getPageNo());
      mv.addObject("size", product.getiDisplayLength());

      mv.addObject("total", productService.count(product));
      ConfValue cv = confService.find(typeid); // 获取分类的父id,查询同级分类
      
      List<ConfValue>  plist = confService.queryByType(GlobalConstant.TYPE_PRODUCT, cv.getPid());// 产品分类
      mv.addObject("productClass", plist);// 根据大分类 查询小分类 列表
      if(0 == cv.getPid()){
        mv.addObject("typeName", cv.getValue0());// 分类的名称 用于 页面导航路径展示
      }else{
        ConfValue pcv = confService.find(cv.getPid()); // 查询父分类名称
        mv.addObject("typeName", pcv.getValue0());// 父分类的名称 用于 页面导航路径展示
      }
        
      mv.addObject("product", product);
      mv.addObject("typeid", typeid);
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "查询文章异常", e);
    }
    return mv;
  }

  /**
   * 产品详细内容页
   * @param id
   * @return
   */
  @RequestMapping("/show-{id}.html")
  public ModelAndView queryDetail(@PathVariable Long id) {
    ModelAndView mv = new ModelAndView("/product/productDetail.jsp");
    try {
      Product product = productService.find(id);
      mv.addObject("product", product);
      
      List<ConfValue>  plist = confService.queryByType(GlobalConstant.TYPE_PRODUCT,null);// 产品分类
      mv.addObject("productClass", plist);// 根据大分类 查询小分类 列表
      
      // 增加一次查看
      if(product!=null) {
        product.setReadTimes(product.getReadTimes()==null?1:product.getReadTimes()+1);
        productService.update(product);
      }
    }catch(Exception e) {
      BaseLog.e(this.getClass(), "产品详细内容页异常", e);
    }

    return mv;
  }

  /**
   * 查询产品列表
   * @param product
   * @return
   */
  @RequestMapping("/myproduct")
  public ModelAndView myproduct(Product product) {
    ModelAndView mv = new ModelAndView("/my/myproductInfo.jsp");
    try {
      Member mbo = (Member) session.getAttribute("user");
      if(mbo == null){
        mv.setViewName("404.jsp");
      }else{
        product.setUserid(mbo.getId());
        //设置分页信息
        if(product.getiDisplayLength() == 0) product.setiDisplayLength(10);                //默认长度5
        if(product.getPageNo()<=0) product.setPageNo(1);                                  //默认页数1
        product.setiDisplayStart((product.getPageNo() - 1) * product.getiDisplayLength());  //显示起始索引
  
        mv.addObject("dataList", productService.query(product));
        mv.addObject("pageNo", product.getPageNo());
        mv.addObject("size", product.getiDisplayLength());
  
        mv.addObject("total", productService.count(product));
        mv.addObject("product", product);
      }
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "查询文章异常", e);
    }
    return mv;
  }
  
  /**
   * 删除产品
   * @param product
   * @return
   */
  @RequestMapping("/delproduct")
  public ModelAndView delproduct(Product product,Long id) {
    ModelAndView mv = new ModelAndView("redirect:/product/myproduct");
    try {
      Member mbo = (Member) session.getAttribute("user");
      if(mbo == null){
        mv.setViewName("404.jsp");
      }else{
        product.setUserid(mbo.getId());
        product.setId(id);
        productService.del(product); 
      }
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "查询文章异常", e);
    }
    return mv;
  }
  
  /**
   * 初始化产品
   * @param product
   * @return
   */
  @RequestMapping("/initproduct")
  public ModelAndView initproduct(Product product,Long id) {
    ModelAndView mv = new ModelAndView("/my/productDetail.jsp");
    mv.addObject("column", this.confService.queryByType(GlobalConstant.TYPE_PRODUCT, 9667L));
    Member mbo = (Member) session.getAttribute("user");
    if(mbo == null){
      mv.setViewName("404.jsp");
    }else{
      product = productService.find(id);
      mv.addObject("data", product);
    }
    return mv;
  }
  
  
  /**
   * 文章类的内容搜索
   * @param product
   * @return
   */
  @RequestMapping("/search")
  public ModelAndView search(Product product,String keywords) {
    ModelAndView mv = new ModelAndView("/searchproductList.jsp");
    try {
      //设置分页信息
      if(product.getiDisplayLength() == 0) product.setiDisplayLength(10);                //默认长度5
      if(product.getPageNo()<=0) product.setPageNo(1);                                  //默认页数1
      product.setiDisplayStart((product.getPageNo() - 1) * product.getiDisplayLength());  //显示起始索引

      product.setStatus("1");
      mv.addObject("dataList", productService.queryByLike(product,keywords));
      mv.addObject("keywords", keywords);
      mv.addObject("pageNo", product.getPageNo());
      mv.addObject("size", product.getiDisplayLength());

      mv.addObject("total", productService.count(product));
      
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "查询文章异常", e);
    }
    return mv;
  }
  
}
